/*
 * Copyright 2009 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gtugs.web.reporting;

import java.util.GregorianCalendar;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * @author jasonacooper@google.com (Jason Cooper)
 */
class Email {

  private StringBuilder text = new StringBuilder();

  public void append(String toAppend) {
    text.append(toAppend);
  }

  public void send() {
    Properties props = new Properties();
    Session session = Session.getDefaultInstance(props, null);

    GregorianCalendar today = new GregorianCalendar();

    try {
      Message msg = new MimeMessage(session);
      msg.setFrom(new InternetAddress("van@gtugs.org"));
      msg.addRecipient(Message.RecipientType.TO,
          new InternetAddress("vanriper@google.com"));
      msg.addRecipient(Message.RecipientType.TO,
          new InternetAddress("stephaniel@google.com"));
      msg.setSubject("gtugs.org chapter status summary for " +
          (today.get(GregorianCalendar.MONTH) + 1) + "/" +
          today.get(GregorianCalendar.DAY_OF_MONTH));
      msg.setText(text.toString());
      Transport.send(msg);
    } catch (AddressException e) {
    } catch (MessagingException e) {
    }
  }
}